package com.kaesun.ufit.weight;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Message;
import android.text.InputType;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaesun.ufit.hardware.Buzzer;
import com.kaesun.ufit.tools.ConfigData;
import com.kaesun.ufit.tools.ToolbarView;

public class WiFiActivity extends Activity implements View.OnClickListener, IOnHandlerMessage {

	private static final int BUTTON_IDS[] = {
		R.id.button_back, R.id.button_home,

		R.id.text_wifi_ssid, R.id.text_wifi_ssid_value,
		R.id.text_wifi_security, R.id.text_wifi_security_value,
		R.id.text_wifi_password, R.id.text_wifi_password_value,
		R.id.text_wifi_server, R.id.text_wifi_server_value,
		R.id.text_wifi_port, R.id.text_wifi_port_value,
	};

	private static final String STR_SECURITY[] = {"OPEN", "WEP", "WPA", "WPAAES", "WPA2AES", "WPA2TKIP", "WPA2"};

	private static final int INPUT_SSID = 1;
	private static final int INPUT_PASSWORD = 2;
	private static final int INPUT_SERVER = 3;
	private static final int INPUT_PORT = 4;

	private static final int CHOICE_SECURITY = 1;

	private MainApplication mApp = null;
	private WeakRefHandler mHandler = null;
	private ToolbarView mToolbarView = null;

	private TextView mSsidView = null;
	private TextView mSecurityView = null;
	private TextView mPasswordView = null;
	private TextView mServerView = null;
	private TextView mPortView = null;
	private AlertDialog mAlertDialog = null;

	private boolean mIsChanged = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_wifi);

		for (int i=0; i<BUTTON_IDS.length; i++) {
			findViewById(BUTTON_IDS[i]).setOnClickListener(this);
		}

		mApp = (MainApplication)getApplication();
		mHandler = new WeakRefHandler((IOnHandlerMessage)this);
		mToolbarView = (ToolbarView)findViewById(R.id.view_toolbar);

		mSsidView = (TextView)findViewById(R.id.text_wifi_ssid_value);
		mSecurityView = (TextView)findViewById(R.id.text_wifi_security_value);
		mPasswordView = (TextView)findViewById(R.id.text_wifi_password_value);
		mServerView = (TextView)findViewById(R.id.text_wifi_server_value);
		mPortView = (TextView)findViewById(R.id.text_wifi_port_value);

		mToolbarView.chage2Button();
		mToolbarView.setTitle("WiFi");

		mSsidView.setText(String.valueOf(ConfigData.getPrefString(this, ConfigData.PREF_WIFI_SSID, ConfigData.DEF_WIFI_SSID)));
		mSecurityView.setText(String.valueOf(ConfigData.getPrefString(this, ConfigData.PREF_WIFI_SECURITY, ConfigData.DEF_WIFI_SECURITY)));
		mPasswordView.setText(String.valueOf(ConfigData.getPrefString(this, ConfigData.PREF_WIFI_PASSWORD, ConfigData.DEF_WIFI_PASSWORD)));
		mServerView.setText(String.valueOf(ConfigData.getPrefString(this, ConfigData.PREF_WIFI_SERVER, ConfigData.DEF_WIFI_SERVER)));
		mPortView.setText(String.valueOf(ConfigData.getPrefInteger(this, ConfigData.PREF_WIFI_PORT, ConfigData.DEF_WIFI_PORT)));
	}

	@Override
	protected void onDestroy() {

		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		if (mIsChanged) {
			mApp.wiFiAssociation();
		}

		super.onPause();
	} 

	@Override
	protected void onResume() {
		mApp.setHandler(mHandler, true);

		super.onResume();
	}

	public void onClick(View arg0) {
		mApp.timeoutReset();

		Buzzer.delay(50);
		switch (arg0.getId()) {
		case R.id.button_back:
			setResult(RESULT_OK);
			finish();
			break;
		case R.id.button_home:
			setResult(RESULT_CANCELED);
			finish();
			break;

		case R.id.text_wifi_ssid:
		case R.id.text_wifi_ssid_value:
			showInputDialog(INPUT_SSID);
			break;
		case R.id.text_wifi_security:
		case R.id.text_wifi_security_value:
			showChoiceDialog(CHOICE_SECURITY);
			break;
		case R.id.text_wifi_password:
		case R.id.text_wifi_password_value:
			showInputDialog(INPUT_PASSWORD);
			break;
		case R.id.text_wifi_server:
		case R.id.text_wifi_server_value:
			showInputDialog(INPUT_SERVER);
			break;
		case R.id.text_wifi_port:
		case R.id.text_wifi_port_value:
			showInputDialog(INPUT_PORT);
			break;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mApp.timeoutReset();

		return super.onTouchEvent(event);
	}

	@Override
	public void handleMessage(Message msg) {
		switch (msg.what) {
		case MainApplication.HANDLER_TIMEOUT_SECOND:
			mToolbarView.setTime(msg.arg1, msg.arg2);
			break;
		case MainApplication.HANDLER_TIMEOUT_ACTIVITY:
			setResult(RESULT_OK);
			finish();
			break;
		}
	}


	private void showInputDialog(int type) {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		LinearLayout linear = (LinearLayout)View.inflate(this, R.layout.dialog_input, null);
		TextView textView = (TextView)linear.findViewById(R.id.text_title);
		final int fType = type;
		final EditText fEditInput = (EditText)linear.findViewById(R.id.edit_input);

		switch (fType) {
		case INPUT_SSID:
			textView.setText("Ssid");
			fEditInput.setText(mSsidView.getText());
			fEditInput.setInputType(InputType.TYPE_CLASS_TEXT);
			break;
		case INPUT_PASSWORD:
			textView.setText("Passowrd");
			fEditInput.setText(mPasswordView.getText());
			fEditInput.setInputType(InputType.TYPE_CLASS_TEXT/* | InputType.TYPE_TEXT_VARIATION_PASSWORD*/);
			break;
		case INPUT_SERVER:
			textView.setText("Server");
			fEditInput.setText(mServerView.getText());
			fEditInput.setInputType(InputType.TYPE_CLASS_TEXT);
			break;
		case INPUT_PORT:
			textView.setText("Port");
			fEditInput.setText(mPortView.getText());
			fEditInput.setInputType(InputType.TYPE_CLASS_NUMBER);
			break;
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(linear)
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				String input = fEditInput.getText().toString();
				int value;

				switch (fType) {
				case INPUT_SSID:
					mSsidView.setText(input);
					ConfigData.setPref(WiFiActivity.this, ConfigData.PREF_WIFI_SSID, input);
					break;
				case INPUT_PASSWORD:
					mPasswordView.setText(input);
					ConfigData.setPref(WiFiActivity.this, ConfigData.PREF_WIFI_PASSWORD, input);
					break;
				case INPUT_SERVER:
					mServerView.setText(input);
					ConfigData.setPref(WiFiActivity.this, ConfigData.PREF_WIFI_SERVER, input);
					break;
				case INPUT_PORT:
					value = Integer.parseInt(input, 10);
					/*if (80 > value) {
						value = 80;
					} else if (100 < value) {
						value = 100;
					}*/
					mPortView.setText(String.valueOf(value));
					ConfigData.setPref(WiFiActivity.this, ConfigData.PREF_WIFI_PORT, value);
					break;
				}

				mIsChanged = true;
			}
		})
		.setNegativeButton("Cancel", null);

		mAlertDialog = builder.create();
		mAlertDialog.show();
	}

	private int selIndex = 0;
	private void showChoiceDialog(int type) {
		if (null != mAlertDialog) {
			if (mAlertDialog.isShowing()) {
				mAlertDialog.dismiss();
			}
			mAlertDialog = null;
		}

		String strIdx = "";

		AlertDialog.Builder builder = null;
		switch (type) {
		case CHOICE_SECURITY:
			strIdx = (String)mSecurityView.getText();
			for (int i=0; i<STR_SECURITY.length; i++) {
				if (0 == STR_SECURITY[i].compareTo(strIdx)) {
					selIndex = i;
					break;
				}
			}
			builder = new AlertDialog.Builder(this);
			builder.setTitle("Choice Security")
			.setSingleChoiceItems(STR_SECURITY, selIndex, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					selIndex = which;
				}
			})
			.setPositiveButton("OK", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					mSecurityView.setText(STR_SECURITY[selIndex]);
					ConfigData.setPref(WiFiActivity.this, ConfigData.PREF_WIFI_SECURITY, STR_SECURITY[selIndex]);

					mIsChanged = true;
				}
			})
			.setNegativeButton("Cancel", null);
			break;
		}

		if (null != builder) {
			mAlertDialog = builder.create();
			mAlertDialog.show();
		}
	}
}
